package com.java3y.sso.handler;

import com.java3y.sso.annotate.InfrawInputCharacterCheck;
import org.apache.commons.lang3.StringUtils;

import java.lang.annotation.Annotation;

public class InfrawInputCharacterCheckImpl implements InfrawInputHandler {

    @Override
    public void handle(Object o, Annotation filedAnnotation,String filedName, String... str) throws Exception {
        String checkStr = (String) o;
        if (filedAnnotation instanceof InfrawInputCharacterCheck){
            InfrawInputCharacterCheck annotation = (InfrawInputCharacterCheck) filedAnnotation;
            if (annotation.type().equals("half")){
                // 是不是half类型 half必须是英文
                if (!StringUtils.isAlpha(checkStr)){
                    throw new Exception(String.format("字段值必须是 英文字母%s",filedName));
                }
            }else {
                if (StringUtils.isAlpha(checkStr)){
                    throw new Exception(String.format("字段值必须是 全角字母%s",filedName));
                }
            }

            // 固定位数
            if(annotation.fomart() == 1){
                if (annotation.length() != checkStr.length()){
                    throw  new Exception(String.format("字段必须是%s个 %s",annotation.length(),filedName));
                }
            }else {
                // 不固定位数
                if (checkStr.length() > annotation.length() ){
                    throw  new Exception(String.format("不能超过 %s字符%s",annotation.length(),filedName));
                }

            }

        }

    }


}
